//
//  LJHAuthorDetailViewController.m
//  HEARME
//
//  Created by liujinhui on 16/1/28.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LJHAuthorDetailViewController.h"
//#import "TOPlayButton.h"
#import "LJHAlbumsDetailCell.h"
#import "LJHAlbumListTableViewCell.h"
#import "LJHAlbumsDetailViewController.h"
#import "LJHPlayerViewController.h"
//#import "GiFHUD.h"
@interface LJHAuthorDetailViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>

@property(nonatomic,retain)UITableView *myTableView;
@property(nonatomic,retain)NSMutableArray *albumArray;
@property(nonatomic,retain)NSMutableArray *trackArray;
@property(nonatomic,retain)UIView *tableHeadView;
@property(nonatomic,assign)CGFloat cellHeight;
@property(nonatomic,assign)NSInteger currentPage;
@property(nonatomic,retain)UIImageView *backgroundImageView;
@property(nonatomic,retain)UIImageView *overImage;
@property(nonatomic,retain)UIImageView *avatorImageView;
@property(nonatomic,retain)UILabel *trackTitle;
@property(nonatomic,retain)UILabel *trackDigest;
@property(nonatomic,retain)FXBlurView *blurView;



@end

@implementation LJHAuthorDetailViewController

- (void)dealloc
{
    [_blurView release];
    [_overImage release];
    [_backgroundImageView release];
    [_tableHeadView release];
    [_trackArray release];
    [_albumArray release];
    [_myTableView release];
    [super dealloc];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[LJHPlayBtn shareButton].musicBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    if ([LJHPlayerViewController sharaMusicPlayer].player.state == STKAudioPlayerStatePlaying) {
        
        
        [[LJHPlayBtn shareButton] startCircle];
    }
    if ([LJHPlayerViewController sharaMusicPlayer].player.state == STKAudioPlayerStatePaused) {
        
        [[LJHPlayBtn shareButton] stopCircle];
        
    }
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //初始化数组
    //    self.albumArray = [NSMutableArray array];
    self.trackArray = [NSMutableArray array];
    [self createSubviews];
    [self getAuthorData];
    [self getData];
    //    self.navigationController.navigationBar.hidden = YES;
    
    UIButton * backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(20, 30 , 20, 30);
    backButton.backgroundColor = [UIColor clearColor];
    [backButton setImage:[UIImage imageNamed:@"btn_back_n@2x.png"] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"btn_back_h@2x.png"] forState:UIControlStateHighlighted];
    [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
}

#pragma mark - 返回按钮方法
-(void)backButtonAction:(UIButton *)btn
{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
    
}

#pragma mark - 创建子视图
-(void)createSubviews
{
    
    //背景
    self.backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT*0.28)];
    self.backgroundImageView.backgroundColor =[UIColor redColor] ;
    self.backgroundImageView.alpha = 0.5;
    [self.view addSubview:self.backgroundImageView];
    [_backgroundImageView release];
    
    
    _blurView = [[FXBlurView alloc]initWithFrame:self.backgroundImageView.bounds];
    [_backgroundImageView addSubview:_blurView];
    _blurView.blurRadius = 100;
    [_blurView release];
    
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStyleGrouped];
    self.myTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.myTableView];
    [_myTableView release];
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    [self.myTableView registerClass:[LJHAlbumsDetailCell class] forCellReuseIdentifier:@"authorTrackCell"];
    
    self.currentPage = 1;
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getData)];
    self.myTableView.footer = footer;
    
    [self creatHeadView];
    self.myTableView.tableHeaderView = self.tableHeadView;
    
    [LJHPlayBtn shareButton].frame =CGRectMake(WIDTH*0.4, HEIGHT-WIDTH*0.2, WIDTH*0.2, HEIGHT*0.2);
    [self.view addSubview :[LJHPlayBtn shareButton]];
    
}

#pragma mark - 创建头视图
-(void)creatHeadView
{
    self.tableHeadView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT*0.25)];
//    self.tableHeadView.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1];
    
    //头像
    self.avatorImageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH*0.8/2 , HEIGHT*0.02, WIDTH*0.2, WIDTH*0.2)];
    self.avatorImageView.center = CGPointMake(WIDTH/2, HEIGHT*0.1);
    self.avatorImageView.backgroundColor = [UIColor whiteColor];
    self.avatorImageView.layer.masksToBounds = YES;
    self.avatorImageView.layer.cornerRadius = WIDTH*0.1;
    self.avatorImageView.layer.borderColor = [UIColor whiteColor].CGColor;
    self.avatorImageView.layer.borderWidth = 1;
    [self.tableHeadView addSubview:self.avatorImageView];
    [_avatorImageView release];
    
    //主播名
    self.trackTitle = [[UILabel alloc] initWithFrame:CGRectMake(self.avatorImageView.frame.origin.x , self.avatorImageView.frame.origin.y + self.avatorImageView.frame.size.height + HEIGHT*0.02, WIDTH*0.5, HEIGHT*0.03)];
    self.trackTitle.backgroundColor = [UIColor clearColor];
    self.trackTitle.textColor = [UIColor whiteColor];
    self.trackTitle.font = [UIFont systemFontOfSize:13 weight:1];
    self.trackTitle.center = CGPointMake(WIDTH/2, HEIGHT*0.18);
    [self.tableHeadView addSubview:self.trackTitle];
    [_trackTitle release];
    
    //简介
    self.trackDigest = [[UILabel alloc] initWithFrame:CGRectMake(self.avatorImageView.frame.origin.x + self.avatorImageView.frame.size.width + 10 ,self.avatorImageView.frame.origin.y + self.avatorImageView.frame.size.height + 5, WIDTH *0.7, HEIGHT*0.03)];
    self.trackDigest.backgroundColor = [UIColor clearColor];
    self.trackDigest.font = [UIFont systemFontOfSize:11 weight:0];
    self.trackDigest.center = CGPointMake(WIDTH/2, HEIGHT*0.22);
    [self.tableHeadView addSubview:self.trackDigest];
    self.trackDigest.textColor = [UIColor darkGrayColor];
    [self.trackDigest release];
}

#pragma mark - 请求主播数据
-(void)getAuthorData
{
    NSString *urlStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/homePage?position=2&toUid=%ld&device=iphone&title=%@", self.uid, self.myTitle];
    [LJHAppTools afnGetRequest:urlStr block:^(id result) {
        
        
        [self.backgroundImageView sd_setImageWithURL:[NSURL URLWithString:[result objectForKey:@"backgroundLogo"]] placeholderImage:PLACEHOLDER];
        [self.avatorImageView sd_setImageWithURL:[NSURL URLWithString:[result objectForKey:@"mobileSmallLogo"]] placeholderImage:PLACEHOLDER];
        self.trackTitle.text = [result objectForKey:@"nickname"];
        self.trackTitle.textAlignment = NSTextAlignmentCenter;
        self.trackDigest.text = [result objectForKey:@"personalSignature"];
        self.trackDigest.textAlignment = NSTextAlignmentCenter;
        
        [self.myTableView reloadData];
//        [GiFHUD dismiss];
    } error:^(NSError *error) {
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"您的设备无网络访问" message:nil preferredStyle:UIAlertControllerStyleAlert];
        
        [self presentViewController:alertC animated:YES completion:^{
            
        }];
        
        UIAlertAction *cancelButton = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        [alertC addAction:cancelButton];
    }];
}

#pragma mark - 请求数据
-(void)getData
{
    NSString *urlStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/track/%ld/%ld/30?device=iPhone",(long)self.uid,self.currentPage];
    [LJHAppTools afnGetRequest:urlStr block:^(id result) {
        
        NSMutableArray *array = [result objectForKey:@"list"];
        for (NSMutableDictionary *dic in array) {
            
            LJHTrackModel *model = [LJHTrackModel modelWithDictionary:dic];
            [self.trackArray addObject:model];
        }
        
        self.currentPage++;
        [self.myTableView.footer endRefreshing];
        [self.myTableView reloadData];
        
    } error:^(NSError *error) {
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"您的设备无网络访问" message:nil preferredStyle:UIAlertControllerStyleAlert];
        
        [self presentViewController:alertC animated:YES completion:^{
            
        }];
        
        UIAlertAction *cancelButton = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        [alertC addAction:cancelButton];

    }];
}

#pragma mark - playButton
-(void)buttonAction:(UIButton *)btn
{
    
    LJHPlayerViewController *player = [LJHPlayerViewController sharaMusicPlayer];
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:player];
    
    [self presentViewController:naVC animated:YES completion:^{
        
    }];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.trackArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LJHAlbumsDetailCell *trackCell = [tableView dequeueReusableCellWithIdentifier:@"authorTrackCell"];
    
    LJHTrackModel *model = [self.trackArray objectAtIndex:indexPath.row];
    
    trackCell.trackModel = model;
    
    self.cellHeight = trackCell.playCount.frame.origin.y + trackCell.playCount.frame.size.height + 10;
    
    return trackCell;
    
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    
    return @"发布的声音";
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return _cellHeight;
}

#pragma mark - 点击跳转页面方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //    if (indexPath.section == 0) {
    //
    //        TOAlbumsDetailViewController *albumDetailVC = [[TOAlbumsDetailViewController alloc] init];
    //        [self.navigationController pushViewController:albumDetailVC animated:YES];
    //
    //    }
    
    //    if (indexPath.section == 1) {
    
    LJHPlayerViewController *player = [LJHPlayerViewController sharaMusicPlayer];
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:player];
    LJHTrackModel *sing = [self.trackArray objectAtIndex:indexPath.row];
    player.song_id = sing.trackId;
    player.allMusicArray = self.trackArray;
    player.indexRow = indexPath.row;
    [self presentViewController:naVC animated:YES completion:^{
        
    }];
    
    //    }
    
    
}

#pragma mark - scrollView协议方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSLog(@"👀%f",scrollView.contentOffset.y);
    if (scrollView.contentOffset.y < 0) {
        
        self.backgroundImageView.frame = CGRectMake(0, 0, WIDTH, HEIGHT*0.28-scrollView.contentOffset.y);
        _blurView.frame = CGRectMake(0, 0, WIDTH, HEIGHT*0.28-scrollView.contentOffset.y);
        _overImage.frame = CGRectMake(0, 0, WIDTH, HEIGHT*0.28-scrollView.contentOffset.y);
        
        _blurView.blurRadius = 1;
    }
    
    if (scrollView.contentOffset.y > 0) {
        
        self.backgroundImageView.frame = CGRectMake(0, -scrollView.contentOffset.y, WIDTH, HEIGHT*0.28);
        
    }
    
    if (scrollView.contentOffset.y == 0) {
        _blurView.blurRadius = 100;
    }
    
    [self.view bringSubviewToFront:self.myTableView];
    [self.view bringSubviewToFront:[LJHPlayBtn shareButton]];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    //    [self.navigationController removeFromParentViewController];
//    [GiFHUD dismiss];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
